package com.jessechanrui.template.common.template.auth.websocket;

/**
 * @author chenrui
 * @version 1.0
 * @description StreamWebSocketConfig
 * @date 2025/8/12
 */
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.HandlerMapping;
import org.springframework.web.reactive.handler.SimpleUrlHandlerMapping;
import org.springframework.web.reactive.socket.WebSocketHandler;
import org.springframework.web.reactive.socket.server.support.WebSocketHandlerAdapter;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class StreamWebSocketConfig {

    private final StreamReactiveWebSocketHandler streamReactiveWebSocketHandler;

    public StreamWebSocketConfig(StreamReactiveWebSocketHandler streamReactiveWebSocketHandler) {
        this.streamReactiveWebSocketHandler = streamReactiveWebSocketHandler;
    }

    // 配置流式响应的WebSocket端点
    @Bean
    public HandlerMapping streamWebSocketHandlerMapping() {
        Map<String, WebSocketHandler> map = new HashMap<>();
        map.put("/ws/stream/{sessionId}", streamReactiveWebSocketHandler);

        SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
        handlerMapping.setOrder(1);
        handlerMapping.setUrlMap(map);
        return handlerMapping;
    }

    @Bean
    public WebSocketHandlerAdapter streamHandlerAdapter() {
        return new WebSocketHandlerAdapter();
    }
}
